package com.sz.ac.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sz.ac.entity.AcGrp;
import com.sz.ac.interfs.IAcGrpService;
import com.sz.ac.interfs.IAcUserService;
import com.sz.ac.model.constant.DefMsgException;
import com.sz.ac.model.constant.ResultData;
import com.sz.ac.model.constant.Vars;
import com.sz.ac.services.mapper.ac.AcGrpMapper;
import com.sz.ac.utils.SessionUtils;
import com.sz.ac.utils.VerifyCodeUtils;

@RestController
@RequestMapping("/grp/")
public class GRPController 
{
	private final Logger logger = LoggerFactory.getLogger(GRPController.class);
	@Resource
	private IAcGrpService grpService;
	@Resource
	private IAcUserService userService;
	
	@GetMapping(value="getCode")
	@ResponseBody
	public void getCode(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		response.setHeader("Pragma", "No-cache"); 
        response.setHeader("Cache-Control", "no-cache"); 
        response.setDateHeader("Expires", 0); 
        response.setContentType("image/jpeg"); 
           
        //生成随机字串 
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4); 
        //存入会话session 
        HttpSession session = SessionUtils.getSession(request); 
        //删除以前的
        session.removeAttribute(Vars.VERIFYCODE);
        session.setAttribute(Vars.VERIFYCODE, verifyCode.toLowerCase());
        //生成图片 
        int w = 100, h = 40; 
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode); 
	}
	
	/**
	 * @param paramMap
	 * @param request
	 * @return
	 * 获取我的战队信息
	 */
	@PostMapping(value="getMyGrp")
	@ResponseBody
	public ResultData getMyGrp(@RequestBody Map<String, String> paramMap,HttpServletRequest request){
		try
		{
			String phone = paramMap.get("phone");
			String verifyCode = paramMap.get("verifyCode");
			HttpSession session = SessionUtils.getSession(request); 
			String inCode = session.getAttribute(Vars.VERIFYCODE)+"";
			if(StringUtils.isEmpty(verifyCode) || !verifyCode.equalsIgnoreCase(inCode))
			{
				logger.info("==verifyCode={}=,sessionCode={}===",verifyCode,inCode);
				return ResultData.error("验证码错误");
			}
			return ResultData.success(grpService.getMyGrp(phone));
		}
		catch(DefMsgException e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error(e.getDefMsg());
		}
		catch(Exception e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error();
		}
	}
	/**
	 * @param paramMap
	 * @param request
	 * @return
	 * 获取所有的战队信息
	 */
	@GetMapping(value="getAllMyGrp")
	@ResponseBody
	public ResultData getAllMyGrp(){
		try
		{
			return ResultData.success(grpService.getAllGrp());
		}
		catch(DefMsgException e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error(e.getDefMsg());
		}
		catch(Exception e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error();
		}
	}
	
	/**
	 * @param paramMap
	 * @return
	 * 创建我的战队
	 */
	@PostMapping(value="createMyGrp")
	public ResultData createMyGrp(@RequestBody Map<String, String> paramMap){
		try
		{
			String phone = paramMap.get("phone");
			if(StringUtils.isEmpty(phone))
			{
				return ResultData.error("手机号错误");
			}
			boolean isNewInvest = grpService.createMyGrp(phone);
			Map<String, Object> retMap = new HashMap<String, Object>();
			retMap.put("isNewInvest", isNewInvest);
			return ResultData.success(retMap);
		}
		catch(DefMsgException e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error(e.getDefMsg());
		}
		catch(Exception e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error();
		}
	}
	
	
	/**
	 * @param paramMap
	 * @return
	 * 进入邀请的战队
	 */
	@PostMapping(value="inGrp")
	public ResultData inGrp(@RequestBody Map<String, Object> paramMap){
		try
		{
			String phone = paramMap.get("phone")+"";
			Integer inviteId = Integer.valueOf(paramMap.get("inviteUserId")+"");
			Integer grpId =  Integer.valueOf(paramMap.get("grpId")+"");
			if(StringUtils.isEmpty(phone)||inviteId == null || grpId==null)
			{
				return ResultData.error("手机号错误");
			}
			grpService.joinGrpByInvteing(phone, inviteId,grpId);
			return ResultData.success("");
		}
		catch(DefMsgException e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error(e.getDefMsg());
		}
		catch(Exception e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error();
		}
	}
	
	/**
	 * @param paramMap
	 * @return
	 * 进入邀请的战队
	 */
	@PostMapping(value="login")
	@ResponseBody
	public ResultData login(@RequestBody Map<String, Object> paramMap,HttpServletRequest request){
		try
		{
			String userName = paramMap.get("userName")+"";
			String pwd = paramMap.get("pwd")+"";
			logger.info("userName={},pwd={}",userName,pwd);
			boolean isLogin = userService.login(userName, pwd);
			if(isLogin){
				HttpSession session = SessionUtils.getSession(request); 
				session.setAttribute(Vars.LOGINKEY, pwd+userName+Math.random());
				return ResultData.success("");
			}else{
				return ResultData.error("用户名或密码错误");
			}	
			
			
		}
		catch(DefMsgException e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error(e.getDefMsg());
		}
		catch(Exception e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error();
		}
	}
	
	
}
